<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2023-06-13
 * Time: 16:35
 */

namespace app\project\controller;

use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class Exposure extends Controller
{

    public function index(){

        $istype = $_GET['istype'];
        $assign['istype'] = $istype;
        $this->assign($assign);
        $libraryArray = [1=>"招商引资库",2=>"项目预备库",3=>"项目开工库",4=>"市重点项目库",5=>"入统库",6=>"双千服务库"];
        if (!empty($_GET['type'])) {
            try {

                $page = empty($_GET['page'])?1:$_GET['page'];
                $limit = empty($_GET['pageSize'])?10:$_GET['pageSize'];
                $start = ($page-1)*$limit;

                $istype = empty($_GET['istype'])?0:$_GET['istype'];
                switch ($istype){
                    case 1: //逾期未签约
                        $thisTime = date("Y-m-d");
                        $where['library'] = 1;
                        $where['sign'] = ['not in',[2,5]];
                        $where['signTime'] = ['lt',$thisTime];
//                        $where['exposureAuto'] = 0;
                        if(!empty($_GET['keyword'])){
                            $where['title'] = ['like',"%".$_GET['keyword']."%"];
                        }
                        if(!empty($_GET['exposureAuto'])){
                            $where['exposureAuto'] = $_GET['exposureAuto']-1;
                        }

                        $list = Db::name("project_pro")->where($where)->limit($start,$limit)->select();
                        $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                        foreach ($list as $k=>$v){
                            $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                        }
                        $count = Db::name("project_pro")->where($where)->count("*");
                        break;
                    case 2: //逾期未开工
                        $thisTime = date("Y-m-d");
                        $where['b.type_id'] = 343;
                        $where['b.status'] = ['neq',1];
                        $where['b.time'] = ['lt',$thisTime];
                        $where['a.library'] = ['in',[2,3]];
                        $where['a.plan_category'] = 2;
                        if(!empty($_GET['keyword'])){
                            $where['a.title'] = ['like',"%".$_GET['keyword']."%"];
                        }
                        if(!empty($_GET['exposureAuto'])){
                            $where['a.exposureAuto'] = $_GET['exposureAuto']-1;
                        }

                        $list = Db::name("project_pro")->alias("a")
                            ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                            ->field("a.*")->where($where)->limit($start,$limit)->select();
                        $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                        $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
                        $warehousing = [1=>"待入库",2=>"已入库"];
                        foreach ($list as $k=>$v){
                            $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                            $list[$k]['startName'] = empty($start_status[$v['start_status']])?"":$start_status[$v['start_status']];
                            $list[$k]['warehousingName'] = empty($warehousing[$v['warehousing']])?"":$warehousing[$v['warehousing']];
                        }
                        $count = Db::name("project_pro")->alias("a")
                            ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                            ->where($where)->count("*");
                        break;
                    case 3: //逾期未入统
                        $thisTime = date("Y-m-d");
                        $where['b.type_id'] = 344;
                        $where['b.status'] = ['neq',1];
                        $where['b.time'] = ['lt',$thisTime];
                        $where['a.library'] = ['in',[3]];
                        $where['a.plan_category'] = 2;
                        if(!empty($_GET['keyword'])){
                            $where['a.title'] = ['like',"%".$_GET['keyword']."%"];
                        }
                        if(!empty($_GET['exposureAuto'])){
                            $where['a.exposureAuto'] = $_GET['exposureAuto']-1;
                        }

                        $list = Db::name("project_pro")->alias("a")
                            ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                            ->field("a.*")->where($where)->limit($start,$limit)->select();
                        $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                        $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
                        $warehousing = [1=>"待入库",2=>"已入库"];
                        foreach ($list as $k=>$v){
                            $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                            $list[$k]['startName'] = empty($start_status[$v['start_status']])?"":$start_status[$v['start_status']];
                            $list[$k]['warehousingName'] = empty($warehousing[$v['warehousing']])?"":$warehousing[$v['warehousing']];
                        }
                        $count = Db::name("project_pro")->alias("a")
                            ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                            ->where($where)->count("*");
                        break;
                    case 4: //逾期未投产
                        $thisTime = date("Y-m-d");
                        $where['b.type_id'] = 350;
                        $where['b.status'] = ['neq',1];
                        $where['b.time'] = ['lt',$thisTime];
                        $where['a.library'] = ['in',[3]];
                        $where['a.plan_category'] = 2;
                        if(!empty($_GET['exposureAuto'])){
                            $where['a.exposureAuto'] = $_GET['exposureAuto']-1;
                        }
                        if(!empty($_GET['keyword'])){
                            $where['a.title'] = ['like',"%".$_GET['keyword']."%"];
                        }

                        $list = Db::name("project_pro")->alias("a")
                            ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                            ->field("a.*")->where($where)->limit($start,$limit)->select();
                        $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                        $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
                        $warehousing = [1=>"待入库",2=>"已入库"];
                        foreach ($list as $k=>$v){
                            $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                            $list[$k]['startName'] = empty($start_status[$v['start_status']])?"":$start_status[$v['start_status']];
                            $list[$k]['warehousingName'] = empty($warehousing[$v['warehousing']])?"":$warehousing[$v['warehousing']];
                        }
                        $count = Db::name("project_pro")->alias("a")
                            ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                            ->where($where)->count("*");
                        break;
                    case 5: //其他节点逾期
                        $thisTime = date("Y-m-d");
                        $where['b.type_id'] = ['not in',[343,344,350]];
                        $where['b.status'] = ['neq',1];
                        $where['b.time'] = ['lt',$thisTime];
                        $where['a.library'] = ['in',[2,3]];
                        $where['a.plan_category'] = 2;
                        if(!empty($_GET['exposureAuto'])){
                            $where['a.exposureAuto'] = $_GET['exposureAuto']-1;
                        }
                        if(!empty($_GET['keyword'])){
                            $where['a.title'] = ['like',"%".$_GET['keyword']."%"];
                        }
                        $list = Db::name("project_pro")->alias("a")
                            ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                            ->field("a.*")->where($where)->limit($start,$limit)->group("a.id")->select();
                        $signArray = [1=>"未签约",2=>"已签约",3=>"已逾期",4=>"暂缓推进",5=>"在谈项目"];
                        $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
                        $warehousing = [1=>"待入库",2=>"已入库"];
                        foreach ($list as $k=>$v){
                            $list[$k]['sinaName'] = empty($signArray[$v['sign']])?"":$signArray[$v['sign']];
                            $list[$k]['startName'] = empty($start_status[$v['start_status']])?"":$start_status[$v['start_status']];
                            $list[$k]['warehousingName'] = empty($warehousing[$v['warehousing']])?"":$warehousing[$v['warehousing']];
                        }
                        $count = Db::name("project_pro")->alias("a")
                            ->join("project_pro_advance_plan b","a.id = b.pro_id or a.pid = b.pro_id")
                            ->where($where)->group("a.id")->count("*");
                        break;
                }

                $common = new Common();
                foreach ($list as $k=>$v){
                    $list[$k]['libraryName'] = $libraryArray[$v['library']];

                    //推进计划未审核
                    $advanceCount = 0;
                    unset($advanceWhere);
                    $advanceWhere['pro_id'] = empty($v['pid'])?$v['id']:$v['pid'];
                    $advanceWhere['content'] = ['neq',''];
                    $advanceWhere['status'] = 0;
                    $advanceCount = $common->public_select("project_pro_advance_plan", $advanceWhere)->count("*");
                    $list[$k]['advanceCount'] = $advanceCount;

                    //获取未审核的督查概况
                    $overseerCount = 0;
                    unset($advanceWhere);
                    $advanceWhere['pro_id'] = $v['id'];
                    $advanceWhere['status'] = 0;
                    $overseerCount = $common->public_select("project_pro_overseer", $advanceWhere)->count("*");
                    $list[$k]['overseerCount'] = $overseerCount;

                }


                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['line'] = $e->getLine();
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        return $this->fetch("index");

    }

}